package com.atguigu.flinkCEP;

import com.atguigu.been.LoginEvent;
import com.atguigu.been.OrderEvent;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.cep.CEP;
import org.apache.flink.cep.PatternSelectFunction;
import org.apache.flink.cep.PatternStream;
import org.apache.flink.cep.PatternTimeoutFunction;
import org.apache.flink.cep.pattern.Pattern;
import org.apache.flink.cep.pattern.conditions.SimpleCondition;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.OutputTag;

import java.time.Duration;
import java.util.List;
import java.util.Map;

/**
 * @author wky
 * @create 2021-07-20-15:54
 */
public class Flink12_CEP_Project_OrderWatch {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment senv = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> streamSource = senv.readTextFile("src/input/OrderLog.csv");

        WatermarkStrategy<OrderEvent> watermarkStrategy = WatermarkStrategy.<OrderEvent>forBoundedOutOfOrderness(Duration.ofSeconds(50))
                .withTimestampAssigner(new SerializableTimestampAssigner<OrderEvent>() {
                    @Override
                    public long extractTimestamp(OrderEvent element, long recordTimestamp) {
                        return element.getEventTime();
                    }
                });
        KeyedStream<OrderEvent, Long> keyedStream = streamSource.map(line -> {
                    String[] datas = line.split(",");
                    return new OrderEvent(
                            Long.valueOf(datas[0]),
                            datas[1],
                            datas[2],
                            Long.parseLong(datas[3]) * 1000);
                }
        ).assignTimestampsAndWatermarks(watermarkStrategy)
                .keyBy(OrderEvent::getOrderId);


        Pattern<OrderEvent, OrderEvent> pattern = Pattern.<OrderEvent>begin("create")
                .where(new SimpleCondition<OrderEvent>() {
                    @Override
                    public boolean filter(OrderEvent value) throws Exception {
                        return "create".equals(value.getEventType());
                    }
                })
                .next("pay")
                .where(new SimpleCondition<OrderEvent>() {
                    @Override
                    public boolean filter(OrderEvent value) throws Exception {
                        return "pay".equals(value.getEventType());
                    }
                })
                .within(Time.minutes(15));
        SingleOutputStreamOperator<String> result = CEP.pattern(keyedStream, pattern).select(new OutputTag<String>("timeout") {
                                                                                             }, new PatternTimeoutFunction<OrderEvent, String>() {
                                                                                                 @Override
                                                                                                 public String timeout(Map<String, List<OrderEvent>> map, long l) throws Exception {
                                                                                                     return map.toString();
                                                                                                 }
                                                                                             }, new PatternSelectFunction<OrderEvent, String>() {
                                                                                                 @Override
                                                                                                 public String select(Map<String, List<OrderEvent>> map) throws Exception {
                                                                                                     return map.toString();
                                                                                                 }
                                                                                             }
        );
        result.getSideOutput(new OutputTag<OrderEvent>("timeout"){}).print("警告");
        result.print("正常");

        senv.execute();


    }
}
